package com.beiding.controller.user;

import com.beiding.exception.BadRequestException;
import com.beiding.exception.RegisterException;
import com.beiding.pojo.auth.User;
import com.beiding.service.email.EmailService;
import com.beiding.service.user.UserService;
import com.beiding.utils.RegexUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@RequestMapping("/user")
public class UserRestController {


    private EmailService emailService;

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }

    //发送验证码
    @PostMapping("/sendCode")
    void sendCode(String email) {
        emailService.sendCode(email);
    }

    //修改密码
    @PostMapping("/change-pw")
    @Secured("ROLE_USER")
    void changePassword(Principal principal, String oldPassword, String newPassword) {

        //由于前端会进行新旧邮箱格式的校验,认为前端不会出现这种问题.
        if (!RegexUtils.matchPassword(newPassword) || !RegexUtils.matchPassword(oldPassword)) {
            throw new BadRequestException("密码格式不规范");
        }

        //检验原密码是否输入正确
        if (!userService.checkPassword(principal.getName(), oldPassword)) {
            throw new BadRequestException("原密码错误");
        }

        //检验两次密码是否相同
        if (oldPassword.equals(newPassword)) {
            throw new BadRequestException("新旧密码相同");
        }

        //执行修改密码
        userService.changePassword(principal.getName(), newPassword);

    }


    //修改邮箱
    @PostMapping("/change-email")
    @Secured("ROLE_USER")
    void changeEmailPost(Principal principal, String newEmail, String code) {

        //前端会进行有个格式的判断.断言邮箱格式不会出错
        if (!RegexUtils.matchEmail(newEmail)) {
            throw new BadRequestException("邮箱格式不规范");
        }


        if (userService.getEmail(principal.getName()).equals(newEmail)) {
            throw new BadRequestException("新旧邮箱相同");
        }


        try {
            emailService.checkCode(newEmail, code);

        } catch (RegisterException registerException) {
            throw new BadRequestException(registerException.getMessage());
        }

        userService.changeEmail(principal.getName(), newEmail);

        //验证码失效
        emailService.invalidateCode(code);

    }


    @PostMapping("/retrieve-password")
    String retrievePassword(String username) {

        //获取邮件地址
        User user = userService.getUser(username);

        //发送找回密码的邮件
        emailService.sendRetrievePassword(user.getEmail(), username);

        return user.getIncompleteEmail();

    }


    @PostMapping("/retrieve-password/new")
    void retrievePasswordPasswordNew(String username, String password, String token) {

        //首先验证密码的格式
        if (!RegexUtils.matchPassword(password)) {
            throw new BadRequestException("密码格式不规范");
        }

        //再检验一次令牌
        String gotUsername = emailService.getUsernameByToken(token);

        if (!username.equals(gotUsername)) {
            throw new BadRequestException("令牌无效或已过期");
        }

        //修改密码
        userService.changePassword(username, password);

        //令牌作废
        emailService.invalidateToken(token);

    }

    //注销当前账户
    @PostMapping("/unsubscribe")
    @Secured("ROLE_USER")
    void unsubscribe(Principal principal, String password) {

        if (!userService.checkPassword(principal.getName(), password)) {
            throw new BadRequestException("密码输入错误");
        }
        userService.unsubscribe(principal);
    }


}
